﻿using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.Supervise.Model;
using HZ.Supervise.IBLL;
using System.Collections.Generic;
using HZ.Supervise.Model.Enum;
using System.Text;
using System.Text.RegularExpressions;

public partial class Company_CompanyInfo_SC : BasePage
{
    public Company_CompanyInfo_SC()
    {
        this.ModuleNo = Module.生产企业汇总统计.ToString();
        this.PageOperateType = OperateType.医疗机构汇总统计.ToString();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!IsPostBack)
        {
            this.AreaControl1.InitAreaID = CurrentAccount.GetUser().Org.Area.AreaID;
            BindData();
            //ViewState["query"] = URLUtil.GetSuperviseQueryFromURL(Request);
            //QueryControl1.BindDataPre((SuperviseQuery)ViewState["query"]);//绑定查询条件的一些数据源 质量层次等
            ////BindData();//绑定数据
            //QueryControl1.BindSort(gvwInfo);
            ////GetCompany_SCCount();
        }
    }

    void QueryControl1_OnQueryClick(object sender, EventArgs e)
    {
        AspNetPager1.CurrentPageIndex = 1;
       
    }


    private SuperviseQuery QueryBy()
    {
        SuperviseQuery vs = new SuperviseQuery();

        #region 查询方法



        //System.Text.StringBuilder sb = new System.Text.StringBuilder();

        if (cbProductNameLike.Checked == true)
        {

            vs.Flag = "1";
        }
        else
        {
            vs.Flag = "0";
        }

        if (HZ.Common.zhHansUtil.HaveChineseCode(txtProductName.Text.Trim()) == true)
        {
            vs.ProductName = this.txtProductName.Text.Trim();
        }
        else
        {
            vs.PinyinCode = this.txtProductName.Text.Trim();

        }
        string timeFrom = string.Intern(this.txtTimeFrom.Text.Trim());

        string timeTo = string.Intern(this.txtTimeTo.Text.Trim());

        if (!string.IsNullOrEmpty(timeFrom))
        {
            vs.CenSubmiitTimeFrom = Convert.ToDateTime(timeFrom);

        }

        if (!string.IsNullOrEmpty(timeTo))
        {
            vs.CenSubmiitTimeTo = Convert.ToDateTime(timeTo);
        }

        if (!string.IsNullOrEmpty(this.txtMedicineModel.Text.Trim()))
        {
            vs.MedicineModelName = txtMedicineModel.Text.Trim();
        }
        if (!string.IsNullOrEmpty(this.txtPack.Text.Trim()))
        {
            vs.Pack = txtPack.Text.Trim();
        }
        if (!string.IsNullOrEmpty(this.txtUnit.Text.Trim()))
        {
            vs.Unit = txtUnit.Text.Trim();

        }
        if (!string.IsNullOrEmpty(this.txtHospitalName.Text.Trim()))
        {
            vs.HospitalName = txtHospitalName.Text.Trim();
        }
        string areaid = AreaControl1.SelectedAreaID;
        if (!string.IsNullOrEmpty(areaid) && areaid != "510000")
        {
            vs.AreaID = areaid;

        }
        else
        {
            vs.AreaID = "22";

        }


        if (!string.IsNullOrEmpty(txtAmountFrom.Text.Trim()))
        {
            vs.WarehouseAmountFrom = Convert.ToDecimal(txtAmountFrom.Text.Trim());


        }
        if (!string.IsNullOrEmpty(txtAmountTo.Text.Trim()))
        {
            vs.WarehouseAmountTo = Convert.ToDecimal(txtAmountTo.Text.Trim());


        }
        string Level = string.Empty;
        for (int i = 0; i < cblboxlist.Items.Count; i++)
        {
            if (cblboxlist.Items[i].Selected == true)
            {
                Level += "'" + cblboxlist.Items[i].Value + "',";
            }
        }


        if (!string.IsNullOrEmpty(Level))
        {
            vs.Levels = Level.Substring(0, Level.Length - 1);

        }


        string isCountry = string.Empty;
        for (int i = 0; i < chkIsCountry.Items.Count; i++)
        {
            if (chkIsCountry.Items[i].Selected == true)
            {
                isCountry += "'" + chkIsCountry.Items[i].Value + "',";
            }
        }


        if (!string.IsNullOrEmpty(isCountry))
        {
            vs.IsCountry = isCountry.Substring(0, isCountry.Length - 1);
        }

        vs.Append = this.OrderFileds.SelectedValue + "  " + this.OrderType.SelectedValue;
        return vs;
        #endregion
    }
    private void BindData()
    {
        HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();
        
       int t = 0;
        System.Data.DataTable dt = ie.CompanySC_InfoByType(QueryBy(), this.AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out t);
      
        AspNetPager1.RecordCount = t;
        gvwInfo.PageSize = AspNetPager1.PageSize;
        this.gvwInfo.DataSource = dt;
        //HZ.Supervise.Web.Function.ChangeRate(dt, "WareHouseRate");
        //HZ.Supervise.Web.Function.ChangeRate_PS(dt, "DistributeRate");
        //HZ.Supervise.Web.Function.ChangeRate_Sell(dt, "SellRate");
        this.gvwInfo.DataBind();


        //this.lblCount.Text = "采购总金额为：" + PurchaseAmountTotal.ToString() + ";" + "入库总金额：" + WareHouseAmountTotal.ToString() + ";" + "销售总金额：" + SellAmountTotal + ";";

    }


    protected void btnQuery_Click(object sender, EventArgs e)
    {
        AspNetPager1.CurrentPageIndex = 1;
    }

    /// <summary>
    /// 翻页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
		//ViewState["query"] = QueryControl1.UpdateQueryFromValue((SuperviseQuery)ViewState["query"]);
		BindData();
		GetCompany_SCCount();
    }


    /// <summary>
    /// 跳转至其它页面，同时将本页面的一些参数传至下一页面
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvwInfo_RowCommand(object sender, GridViewCommandEventArgs e)
    {
       
    }

    protected void ddlOther_SelectedIndexChanged(object sender, EventArgs e)
    {
        string stkey = "";
        string sessionid = string.Empty;
        GridViewRow row = (GridViewRow)((Control)sender).Parent.Parent;
        DropDownList ddl = (DropDownList)row.FindControl("ddlOther");
        stkey = ddl.SelectedValue;
        if (stkey != "")
        {
            sessionid = Guid.NewGuid().ToString();
            SuperviseQuery query = (SuperviseQuery)ViewState["query"];
            //赋予当前行的主键值
            //query.CompanyName_SC = row.Cells[1].Text; //((LinkButton)row.FindControl("lbnHos")).CommandArgument;
            query.CompanyName_SC = gvwInfo.DataKeys[row.RowIndex].Value.ToString();
            query._QueryPrompt = query._QueryPrompt + " " + gvwInfo.DataKeys[row.RowIndex].Value.ToString();

            Session[sessionid] = query;
            string url = System.Configuration.ConfigurationManager.AppSettings[stkey];
            Response.Redirect(string.Format("{0}?sessionid={1}", url, sessionid));
        }
    }


    protected void btnExport_Click(object sender, EventArgs e)
    {
        HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();

        int t = 0;
        System.Data.DataTable dt = ie.CompanySC_InfoByType(QueryBy(), 1, 0, out t);

        ExportFile.Instance.ExportData(dt, gvwInfo, this.Page);
    }
    protected void lbtnSee_Click(object sender, EventArgs e)
    {
        if (this.lbtnSee.Text == "显示图表")
        {
            ChartControl1.Visible = true;
            this.lbtnSee.Text = "隐藏图表";
        }
        else
        {
            ChartControl1.Visible = false;
            this.lbtnSee.Text = "显示图表";
        }
    }
    public void GetCompany_SCCount()
    {
        //int t = 0;
        //HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();
        //ie.QueryObject = new HZ.Supervise.Model.QueryObject()
        //{
        //    //分组字段
        //    GroupField = GroupByField.CompanyName_SC,
        //    //查询条件
        //    Query = (SuperviseQuery)ViewState["query"],
        //    //分组查询字段
        //    GroupSelectField = GroupFields.PurchaseAmountTotal | GroupFields.DistributeAmountTotal | GroupFields.DistributeRate | GroupFields.WareHouseAmountTotal | GroupFields.WareHouseRate | GroupFields.DrugCount,
        //    //排序字段
        //    SortField = QueryControl1.SortField == "" ? "CompanyName_SC ASC" : QueryControl1.SortField
        //};

       
        //System.Data.DataTable dt = ie.ExecutePage(1, 0, out t);
        //int i = dt.Rows.Count;
        //double PurchaseAmountTotal = 0;//采购总金额
        //double DistributeAmountTotal = 0;//配送总金额
        //double WareHouseAmountTotal = 0; //入库总金额
        //int DrugCount = 0;//品规
        //if (i > 0)
        //{
        //    for (int j = 0; j < i; j++)
        //    {
        //        DrugCount += Convert.ToInt32(dt.Rows[j]["DrugCount"].ToString());
        //        PurchaseAmountTotal += HZ.MathUtil.SToF(dt.Rows[j]["PurchaseAmountTotal"].ToString());
        //        DistributeAmountTotal += HZ.MathUtil.SToF(dt.Rows[j]["DistributeAmountTotal"].ToString());
        //        WareHouseAmountTotal += HZ.MathUtil.SToF(dt.Rows[j]["WareHouseAmountTotal"].ToString());
        //    }

        //    lblCount.Text = "品规总数为:" + DrugCount.ToString() + ";采购总金额为:" + PurchaseAmountTotal.ToString("N2") + ";配送总金额为:" + DistributeAmountTotal.ToString("N2") + ";入库总金额为:" + WareHouseAmountTotal.ToString("N2") + "";
        //}
        //else
        //{
        //    lblCount.Text = "暂无符合条件的数据！";
        //}
    }
}
